<template>
  <div class="container">
    <div class="list">
      <ul>
        <li>
          <div class="name">
            顾客自动成为{{appInfo.memberShareAlias || '分销商'}}
          </div>
          <div class="info">
            <van-switch
              :checked="checked"
              active-color="#12cf66"
              @change="onChange"
              size="24px"
            />
          </div>
        </li>
      </ul>
    </div>
    <div v-if="getShowBuyCard" class="list">
      <ul>
        <li @click="authPage()">
          <div class="name">
            提现设置
          </div>
          <div class="info">
            <span v-if="showSettingBankCard">未设置</span>
            <img v-if="fileSpace" :src="fileSpace + '/images/access-icon.png'" alt="" mode="widthFix">
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getMemberInfo } from '@/api/home'
import { getMemberStoreByMemberId, saveMemberStore } from '@/api/personal'
export default {
  components: { },
  data() {
    return {
      memberInfo: {},
      showSettingBankCard: false,
      storeCardFlag: null,
      checked: false,
      memberStore: {},
      shareFlag: 0,
      appInfo: {}
    }
  },
  onShow() {
    // 没有获取到登录信息
    if (!this.getCustomerId || this.getVeekrayStatus === '') {
      this.$pt.reLaunch({ url: '/pages/home/main' })
      return false
    }
    this.getMemberStoreByMemberId()
    this.getMemberInfo()
    this.appInfo = this.$pt.getStorageSync('appInfo')
    let storeCardFlag = this.$pt.getStorageSync('storeCardFlag')
    if (storeCardFlag) {
      this.storeCardFlag = storeCardFlag
    }
    this.$pt.removeStorageSync('storeLogo')
    this.$pt.removeStorageSync('__positiveUrl__')
    this.$pt.removeStorageSync('__reverseUrl__')
  },
  methods: {
    authPage() {
      if (this.getPayMethod === 2) {
        this.$pt.navigateTo({url: '/pages/home/verify-auth/allin-auth/main?pageFrom=list'})
      } else {
        this.$pt.navigateTo({url: '/pages/home/verify-auth/main?pageFrom=list'})
      }
    },
    getMemberInfo() {
      getMemberInfo(this.getCustomerId).then(response => {
        if (response.isError) return
        this.memberInfo = response.data
        this.memberInfo.nickname = this.$utils.getStrByLength(this.memberInfo.nickname, 18)
        // veekrayStatus 萤火微客注册状态 -1:未注册 0:免费名片 1:付费名片
        // this.memberInfo.veekrayStatus = 0
        if (this.memberInfo.veekrayStatus === 1) {
          this.freeBoolean = false
          this.statusIs = true
        } else {
          this.freeBoolean = true
          this.statusIs = false
        }
        if (!this.memberInfo.authFlag) {
          this.showSettingBankCard = true
        }
      })
    },
    getMemberStoreByMemberId() {
      // 获取是否开启默认设置顾客为分销商
      getMemberStoreByMemberId(this.getCustomerId).then(response => {
        if (response.isError) return
        if (response.data) {
          this.memberStore = response.data
          if (response.data.shareFlag === 1) {
            this.checked = true
          } else {
            this.checked = false
          }
        }
      })
    },
    saveMemberStore() {
      let params = Object.assign({}, this.memberStore)
      params.memberId = this.getCustomerId
      params.shareFlag = this.shareFlag
      saveMemberStore(params).then(response => {
        if (response.isError) return
        this.getMemberStoreByMemberId()
      })
    },
    onChange(event) {
      // switch开关变动事件
      if (!event.mp.detail) {
        this.$pt.showModal({
          title: '温馨提示',
          confirmText: '确认关闭',
          content: `关闭后您的顾客成功分销商品不会成为${this.appInfo.memberShareAlias || '分销商'}`,
          success: res => {
            if (res.confirm) {
              this.shareFlag = 0
              this.saveMemberStore()
            }
          }
        })
      } else {
        this.$pt.showModal({
          title: '温馨提示',
          confirmText: '确认开启',
          content: `开启后您所有的顾客成功分销一笔订单后就成为${this.appInfo.memberShareAlias || '分销商'}`,
          success: res => {
            if (res.confirm) {
              this.shareFlag = 1
              this.saveMemberStore()
            }
          }
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.list {
  ul {
    background: #fff;
    margin-top: 10px;
    padding: 0px 10px 0 10px;
  }
  li {
    background-color: #fff;
    height: 50px;
    padding-left: 10px;
    padding-right: 10px;
    display: flex;
    justify-content: space-between;
    border-top: 1px solid #ebebeb;
    &:first-child {
      border-top: none;
    }
    .name {
      font-size: 15px;
      color: #333333;
      display: flex;
      align-items: center;
      height: 100%;
      .icon {
        width: 19px;
        height: auto;
        margin-right: 13px;
      }
    }
    .info {
      font-size: 12px;
      color: #999999;
      display: flex;
      align-items: center;
      height: 100%;
      image {
        width: 5.5px;
        height: auto;
        margin-left: 15px;
      }
    }
  }
}
.list-item {
  background-color: #fff;
  padding-top: 11px;
  ul {
    flex-wrap: wrap;
  }
  li {
    width: 25%;
    height: 75px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    font-size: 12px;
    color: #333333;
    .icon {
      width: 25px;
      height: auto;
      margin-bottom: 9px;
    }
  }
}
</style>
